package cn.iocoder.an.module.business.controller.admin.maintenanceinformation;

import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.EquipmentImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.maintenanceinformation.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.maintenanceinformation.MaintenanceInformationDO;
import cn.iocoder.an.module.business.service.maintenanceinformation.MaintenanceInformationService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 维保商档案")
@RestController
@RequestMapping("/business/maintenance-information")
@Validated
public class MaintenanceInformationController {

    @Resource
    private MaintenanceInformationService maintenanceInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建维保商档案")
    @PreAuthorize("@ss.hasPermission('business:maintenance-information:create')")
    public CommonResult<Long> createMaintenanceInformation(@Valid @RequestBody MaintenanceInformationSaveReqVO createReqVO) {
        return success(maintenanceInformationService.createMaintenanceInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维保商档案")
    @PreAuthorize("@ss.hasPermission('business:maintenance-information:update')")
    public CommonResult<Boolean> updateMaintenanceInformation(@Valid @RequestBody MaintenanceInformationSaveReqVO updateReqVO) {
        maintenanceInformationService.updateMaintenanceInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维保商档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:maintenance-information:delete')")
    public CommonResult<Boolean> deleteMaintenanceInformation(@RequestParam("id") Long id) {
        maintenanceInformationService.deleteMaintenanceInformation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除维保商档案")
                @PreAuthorize("@ss.hasPermission('business:maintenance-information:delete')")
    public CommonResult<Boolean> deleteMaintenanceInformationList(@RequestParam("ids") List<Long> ids) {
        maintenanceInformationService.deleteMaintenanceInformationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维保商档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:maintenance-information:query')")
    public CommonResult<MaintenanceInformationRespVO> getMaintenanceInformation(@RequestParam("id") Long id) {
        MaintenanceInformationDO maintenanceInformation = maintenanceInformationService.getMaintenanceInformation(id);
        return success(BeanUtils.toBean(maintenanceInformation, MaintenanceInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维保商档案分页")
    @PreAuthorize("@ss.hasPermission('business:maintenance-information:query')")
    public CommonResult<PageResult<MaintenanceInformationRespVO>> getMaintenanceInformationPage(@Valid MaintenanceInformationPageReqVO pageReqVO) {
        PageResult<MaintenanceInformationDO> pageResult = maintenanceInformationService.getMaintenanceInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaintenanceInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维保商档案 Excel")
    @PreAuthorize("@ss.hasPermission('business:maintenance-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaintenanceInformationExcel(@Valid MaintenanceInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaintenanceInformationDO> list = maintenanceInformationService.getMaintenanceInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维保商档案.xlsx", "数据", MaintenanceInformationRespVO.class,
                        BeanUtils.toBean(list, MaintenanceInformationRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得设维保商档案导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<MaintenanceInfoImportExcelVO> list = Arrays.asList(
                MaintenanceInfoImportExcelVO.builder().enterpriseName("xx设备有限公司").simpleName("xx公司").creditCode("58856523465232").enterpriseCode("NB4565625386652")
                        .legalRepresentative("张三").companyWebsite("https://www.an.com").contactPerson("李四").mobilePhone("18888888888").landlinePhone("0857-62245125")
                        .faxNumber("265486562").email("an.@mail").entryDate(new Date()).status(MaintenanceStatusEnum.run.getStatus()).bankName("鄞州银行").bankAccount("6227896541356545")
                        .taxNumber("843265869594").invoiceTitle("xxxxxx").registeredAddress("浙江省宁波市").companyAddress("浙江省宁波市鄞州区").businessScope("软件开发").enterpriseInformation("公司成立于xxxxx").build()
        );
        // 输出
        ExcelUtils.write(response, "维保商导入模板.xlsx", "维保商档案列表", MaintenanceInfoImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入维保商档案")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('business:equipment-manage:import')")
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<MaintenanceInfoImportExcelVO> list = ExcelUtils.read(file, MaintenanceInfoImportExcelVO.class);
        return success(maintenanceInformationService.importDataList(list, updateSupport));
    }

}